<template>
  <div class="components-wallet-manage">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="nickName"
            :label="$t('distributionMsg.nickName')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('distribUserWallet.mobileTelNumber')+':'"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distribUserWallet.mobileTelNumber')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetForm"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="basicTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            :label="$t('distribUserWallet.distributorNickname')"
            prop="nickName"
          >
            <template #default="scope">
              <span class="table-cell-text"> {{ scope.row.distributionUser ? scope.row.distributionUser.nickName : "" }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distribUserWallet.mobileTelNumber')"
            prop="userMobile"
          >
            <template #default="scope">
              {{
                scope.row.distributionUser.userMobile
              }}
            </template>
          </el-table-column>
          <!-- 状态 -->
          <el-table-column
            :label="$t('distribUserWallet.status')"
            prop="state"
            align="center"
          >
            <template #default="{row}">
              <!-- -1:封禁 0:待审核 1:正常 2:清退 3:审核未通过，需要重新中请 -->
              <div
                v-if="row.state === -1"
                class="tag-text"
              >
                {{ $t("distribUserWallet.ban") }}
              </div>
              <div
                v-if="row.state === 0"
                class="tag-text"
              >
                {{ $t("product.pendingReview") }}
              </div>
              <div
                v-if="row.state === 1"
                class="tag-text"
              >
                {{ $t("publics.normal") }}
              </div>
              <div
                v-if="row.state === 2"
                class="tag-text"
              >
                {{ $t("distribUserWallet.liquidation") }}
              </div>
              <div
                v-if="row.state === 3"
                class="tag-text"
              >
                {{ $t("publics.unPassed") }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distribUserWallet.waitTosettled')"
            prop="unsettledAmount"
          />
          <el-table-column
            :label="$t('distribUserWallet.withdrawalAmount')"
            prop="settledAmount"
          />
          <el-table-column
            :label="$t('distribUserWallet.invalidAmount')"
            prop="invalidAmount"
          />
          <el-table-column
            :label="$t('distribution.accumulateIncome')"
            prop="addupAmount"
          />
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('admin:distributionUserWallet:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t("crud.updateBtn") }}
                </div>
                <div
                  v-if="isAuth('distribution:distributionUserWallet:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row.walletId)"
                >
                  {{ $t("text.delBtn") }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
    <WalletUpdate
      v-if="walletUpdateVisible"
      ref="walletUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import WalletUpdate from './update.vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { isAuth } from '@/utils/index'

let tempSearchForm = null // 保存上次点击查询的请求条件

const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  userMobile: '',
  nickName: null
})

const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/distribution/distributionUserWallet/pageAndUser'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam == null ? page.currentPage : pageParam.currentPage,
      size: pageParam == null ? page.pageSize : pageParam.pageSize
    }, tempSearchForm))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
  })
}
onMounted(() => {
  getDataList()
})

// 修改提现金额
const walletUpdateRef = ref(null)
const walletUpdateVisible = ref(false)
const onAddOrUpdate = (val) => {
  walletUpdateVisible.value = true
  nextTick(() => {
    walletUpdateRef.value?.init(val)
  })
}
const onDelete = (id) => {
  ElMessageBox.confirm($t('scoreProdList.isDelete'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/distribution/distributionUserWallet/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  }).catch(() => { })
}
// 刷新回调
const refreshChange = () => {
  // page = crud.$refs.tablePageRef.value?.defaultPage
  getDataList(page)
}
const onSearch = (newData = false) => {
  getDataList(page, newData)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

// 重置搜索表单
const searchFormRef = ref(null)
const onResetForm = () => {
  searchFormRef.value.resetFields()
}

</script>
